home *** CD-ROM | disk | FTP | other *** search
/ Practical Algorithms for Image Analysis / Practical Algorithms for Image Analysis.iso / TARFILE.GZ / tarfile / ch_3.5 / bcd / bcd.c next >
Encoding:
C/C++ Source or Header  |  1999-09-11  |  3.4 KB  |  151 lines

  1. /*
  2.  * This program is used with permission from I. Cox.
  3.  * Please reference:
  4.  * R. A. Boie, I. Cox, Proc. IEEE 1st Int. Conf. Computer Vision,
  5.  * London, 1987, pp. 450-456.
  6.  */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <malloc.h>
  10. #include <math.h>
  11. #if defined(WIN32)
  12. #include <io.h>
  13. #endif
  14. #include "edge_finder.h"
  15. #include "ip.h"
  16.  
  17. #define UBYTE_HD 0x4500
  18.  
  19. #define    ON        1
  20. #define    OFF        0
  21.  
  22. extern struct image *my_image;
  23. extern short tiffInput;         /* flag=0 if no ImageIn to set tags; else =1 */
  24. extern char *optarg;
  25. extern int optind, opterr;
  26.  
  27. /*
  28.  * usage of routine
  29.  */
  30. void
  31. usage (char *progname)
  32. {
  33.   progname = last_bs (progname);
  34.   printf ("USAGE: %s inimg outimg sigma [-h][-t][-T thld][-L]\n", progname);
  35.   printf ("\n%s detects and marks contours of regions in a grayscale image\n", progname);
  36.   printf ("by invoking Boie-Cox algorithm\n\n");
  37.   printf ("ARGUMENTS:\n");
  38.   printf ("    inimg: input image filename (TIF)\n");
  39.   printf ("   outimg: output image filename (TIF)\n");
  40.   printf ("    sigma: width of Gaussian filter (float)\n\n");
  41.   printf ("OPTIONS:\n");
  42.   printf ("     -h : do not apply hysteresis\n");
  43.   printf ("     -t : do not thin image\n");
  44.   printf ("-T thld : threshold value (int)\n");
  45.   printf ("     -L : print Software License for this module\n");
  46.   exit (1);
  47. }
  48.  
  49. void
  50. main (int argc, char *argv[])
  51. {
  52.   int nx, ny;
  53.   static unsigned char *pic;
  54.   float sigma;
  55.   int threshold = 1;
  56.   int hysteresis = 1;
  57.   int thinning = 1;
  58.   int i, j;
  59.  
  60.   unsigned char *edge_map;
  61.   unsigned char *image_find_edges (float, int *);
  62.   unsigned char *image_hysteresis ();
  63.   unsigned char *image_thin ();
  64.   Image *imgIn;
  65.   char *input_file, *output_file;
  66.   unsigned char **imgPtr;
  67.   int i_arg;
  68.  
  69. /* 
  70.  * cmd line options:
  71.  */
  72.   static char *optstring = "htT:L";
  73.  
  74. /*
  75.  * parse command line
  76.  */
  77.   optind = 4;
  78.   opterr = ON;                  /* give error messages */
  79.  
  80.   if (argc < 4)
  81.     usage (argv[0]);
  82.  
  83.   while ((i_arg = getopt (argc, argv, optstring)) != EOF) {
  84.     switch (i_arg) {
  85.     case 'h':
  86.       hysteresis = 0;
  87.       break;
  88.     case 't':
  89.       thinning = 0;
  90.       break;
  91.     case 'T':
  92.       threshold = atoi (optarg);
  93.       break;
  94.     case 'L':
  95.       print_bcd_lic ();
  96.       exit (0);
  97.     default:
  98.       usage (argv[0]);
  99.       break;
  100.     }
  101.   }
  102.  
  103.   input_file = argv[1];
  104.   output_file = argv[2];
  105.   sigma = (float) atof (argv[3]);
  106. /*
  107.  * read input image
  108.  */
  109.   imgIn = ImageIn (input_file);
  110.   if (imgIn->bps == 8 && imgIn->spp == 3) {
  111.     printf ("Got RGB image!!!\nInput image must be Grayscale or B&W!!\n");
  112.     exit (1);
  113.   }
  114.  
  115.  
  116.   imgPtr = ImageGetPtr (imgIn);
  117.   nx = imgIn->width;
  118.   ny = imgIn->height;
  119. /*
  120.  * now read in image
  121.  */
  122.   fprintf (stderr, "\nimage size is %d x %d\n", nx, ny);
  123.  
  124.   if ((pic = (unsigned char *) malloc (nx * ny * sizeof (unsigned char))) == 0) {
  125.     fprintf (stderr,
  126.              "%s: ERROR cannot allocate image array\n", argv[0]);
  127.     exit (1);
  128.   }
  129.  
  130.   for (i = 0; i < ny; i++)
  131.     for (j = 0; j < nx; j++)
  132.       *(pic + (i * nx + j)) = imgPtr[i][j];
  133.  
  134.   image_init (pic, nx, ny);
  135.  
  136.   edge_map = image_find_edges (sigma, &threshold);
  137.   if (hysteresis) {
  138.     fprintf (stderr, "\napplying hysteresis\n");
  139.     edge_map = image_hysteresis (threshold / 3);
  140.   }
  141.   if (thinning) {
  142.     fprintf (stderr, "\nthinning image\n");
  143.     edge_map = image_thin ();
  144.   }
  145.   for (i = 0; i < ny; i++)
  146.     for (j = 0; j < nx; j++)
  147.       imgPtr[i][j] = *(edge_map + (i * nx + j));
  148.   ImageOut (output_file, imgIn);
  149.   image_cleanup ();
  150. }
  151.